package com.dmyang.p2pchat.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dmyang.p2pchat.R;
import com.dmyang.p2pchat.bean.Message;
import com.dmyang.p2pchat.bean.User;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by brss on 2017/11/2.
 */

public class ContactsListAdapter extends BaseAdapter {

    private Context context;//上下文
    private List list;//数据列表

    public ContactsListAdapter (Context context, List list){
        this.list = list;
        this.context = context;
    }

    public void refresh(List list) {
        this.list = list;//传入list，然后调用notifyDataSetChanged方法
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        View view = null;

        // 复用view
        if (convertView != null ){
            view = convertView;
        }else {
            //参数1：context,上下文，参数2：resource,要转换成view对象的layout的id，第三个参数：root,将layout用root包一层作为getView的返回值,一般传null。
            view = View.inflate(context, R.layout.item_contracts_list_layout,null);//将一个布局文件转换成一个view对象
        }

        //获取view中的控件

        TextView ipAndPort = view.findViewById(R.id.item_ip_port);
        TextView username4ContractsList = view.findViewById(R.id.item_username4contracts_list);

        //获取postion位置条目对应的list集合中的数据，Bean对象

        User user = (User) list.get(i);

        String ip_Port = user.getIp()+":"+user.getPort();
        ipAndPort.setText(ip_Port);
        username4ContractsList.setText(user.getUsername());

        return view;
    }
}
